package cn.mengfly.fxtest.app;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Pair;
import javafx.util.StringConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mengfly
 * @date 2021/5/27 18:49
 */
public class ChoicesApp extends Application {

    private final static Pair<String, String> EMPTY_PAIR = new Pair<>("", "");



    private final ChoiceBox<Pair<String, String>> assetClass = new ChoiceBox<>();

    @Override
    public void start(Stage primaryStage) throws Exception {
        Label label = new Label("Asset Class:");
        assetClass.setPrefWidth(200);

        Button button = new Button("Save");
        HBox hBox = new HBox(label, assetClass, button);
        hBox.setAlignment(Pos.CENTER);
        hBox.setPadding(new Insets(40));
        hBox.setSpacing(10);

        Scene scene = new Scene(hBox);
        initChoice();

        button.setOnAction(event -> System.out.println("Saving " + assetClass.getValue()));
        primaryStage.setTitle("ChoiceApp");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void initChoice() {
        List<Pair<String, String>> assetClasses = new ArrayList<>();
        assetClasses.add(new Pair<>("Equipment", "20000"));
        assetClasses.add(new Pair<>("Furniture", "21000"));
        assetClasses.add(new Pair<>("Investment", "22000"));

        assetClass.setConverter(new StringConverter<Pair<String, String>>() {
            @Override
            public String toString(Pair<String, String> object) {
                return object.getKey();
            }

            @Override
            public Pair<String, String> fromString(String string) {
                return null;
            }
        });
        assetClass.getItems().add(EMPTY_PAIR);
        assetClass.getItems().addAll(assetClasses);
        assetClass.setValue(EMPTY_PAIR);
    }
}
